import { _decorator, AudioClip, Button, Component, Node, ProgressBar } from 'cc';
import { LighAnimation } from './LighAnimation';
import { FrontBg } from './FrontBg';
import { Role } from './Role';
import { RoundTextTip } from './RoundTextTip';
import { EnterRole } from './EnterRole';
import MainFsm from './fsm/MainFsm';
import { Constant } from './Constant';
import PrepareState from './fsm/state/PrepareState';
import FirstRecruitStartState from './fsm/state/FirstRecruitStartState';
import FirstRecruitFinishState from './fsm/state/FirstRecruitFinishState';
import ClearFirstRecruitState from './fsm/state/ClearFirstRecruitState';
import SecondRecruitPrepareState from './fsm/state/SecondRecruitPrepareState';
import SecondRecruitStartState from './fsm/state/SecondRecruitStartState';
import SecondRecruitFinishState from './fsm/state/SecondRecruitFinishState';
import ClearSecondRecruitState from './fsm/state/ClearSecondRecruitState';
import FirstRoleEnterState from './fsm/state/FirstRoleEnterState';
import SecondRoleEnterState from './fsm/state/SecondRoleEnterState';
import SecondRecruit1PrepareState from './fsm/state/SecondRecruit1PrepareState';
import SecondRecruit1StartState from './fsm/state/SecondRecruit1StartState';
import SecondRecruit1FinishState from './fsm/state/SecondRecruit1FinishState';
import ClearSecondRecruit1State from './fsm/state/ClearSecondRecruit1State';
import SecondRole1EnterState from './fsm/state/SecondRole1EnterState';
import LuckRecruitStartState from './fsm/state/LuckRecruitStartState';
import LuckRecruitPrepareState from './fsm/state/LuckRecruitPrepareState';
import LuckRecruitFinishState from './fsm/state/LuckRecruitFinishState';
import ClearLuckRecruitState from './fsm/state/ClearLuckRecruitState';
import LuckRoleEnterState from './fsm/state/LuckRoleEnterState';
import LastRecruitPrepareState from './fsm/state/LastRecruitPrepareState';
import LastRecruitStartState from './fsm/state/LastRecruitStartState';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {
    @property(AudioClip)
    public bgm: AudioClip;
    @property
    public bgmEnableState: number = 0; //0禁用背景音乐 1 开启背景音乐

    @property(AudioClip)
    public buttonClick: AudioClip;
    @property(AudioClip)
    public heroEnter: AudioClip;
    @property(Button)
    public btnRecruit: Button;

    @property(Button)
    public btnRecruit1: Button;
    @property(Button)
    public btnRecruit2: Button;
    @property(Button)
    public btnRecruit3: Button;
    @property(Button)
    public btnRecruit4: Button;
    @property(LighAnimation)
    public lightAnimation: LighAnimation;
    @property(FrontBg)
    public frontBg: FrontBg;
    @property({ type: [Role] })
    public firstRoundRoles: Role[] = [];
    @property({ type: [Role] })
    public secRoundRoles: Role[] = [];
    @property({ type: [RoundTextTip] })
    public roundTips: RoundTextTip[] = [];
    @property({ type: [Node] })
    public roundOptions: Node[] = [];
    @property({ type: [EnterRole] })
    public enterRolesRoles: EnterRole[] = [];
    @property(ProgressBar)
    public progress: ProgressBar;

    private _mainFsm: MainFsm;

    public get mainFsm() {
        return this._mainFsm;
    }

    start() {
        if (this.bgmEnableState > 0) {
            AudioMgr.inst.play(this.bgm);
        }
        this.btnRecruit.node.on(Button.EventType.CLICK, this.onRecruitClick, this);
        this.btnRecruit1.node.on(Button.EventType.CLICK, this.onRecruit1Click, this);
        this.btnRecruit2.node.on(Button.EventType.CLICK, this.onRecruit2Click, this);
        this.btnRecruit3.node.on(Button.EventType.CLICK, this.onRecruit3Click, this);
        this.btnRecruit4.node.on(Button.EventType.CLICK, this.onRecruit4Click, this);


        this._mainFsm = new MainFsm();
        this._mainFsm.addState(Constant.MainState.Prepare, new PrepareState(), this);
        this._mainFsm.addState(Constant.MainState.FirstRecruitStart, new FirstRecruitStartState(), this);
        this._mainFsm.addState(Constant.MainState.FirstRecruitFinish, new FirstRecruitFinishState(), this);
        this._mainFsm.addState(Constant.MainState.ClearFirstRecruit, new ClearFirstRecruitState(), this);
        this._mainFsm.addState(Constant.MainState.FirstRoundRoleEnter, new FirstRoleEnterState(), this);

        this._mainFsm.addState(Constant.MainState.SecondRecruitPrepare, new SecondRecruitPrepareState(), this);
        this._mainFsm.addState(Constant.MainState.SecondRecruitStart, new SecondRecruitStartState(), this);
        this._mainFsm.addState(Constant.MainState.SecondRecruitFinish, new SecondRecruitFinishState(), this);
        this._mainFsm.addState(Constant.MainState.ClearSecondRecruit, new ClearSecondRecruitState(), this);
        this._mainFsm.addState(Constant.MainState.SecondRoleEnter, new SecondRoleEnterState(), this);

        this._mainFsm.addState(Constant.MainState.SecondRecruit1Prepare, new SecondRecruit1PrepareState(), this);
        this._mainFsm.addState(Constant.MainState.SecondRecruit1Start, new SecondRecruit1StartState(), this);
        this._mainFsm.addState(Constant.MainState.SecondRecruit1Finish, new SecondRecruit1FinishState(), this);
        this._mainFsm.addState(Constant.MainState.ClearSecondRecruit1, new ClearSecondRecruit1State(), this);
        this._mainFsm.addState(Constant.MainState.SecondRole1Enter, new SecondRole1EnterState(), this);

        this._mainFsm.addState(Constant.MainState.LuckRecruitPrepare, new LuckRecruitPrepareState(), this);
        this._mainFsm.addState(Constant.MainState.LuckRecruitStart, new LuckRecruitStartState(), this);
        this._mainFsm.addState(Constant.MainState.LuckRecruitFinish, new LuckRecruitFinishState(), this);
        this._mainFsm.addState(Constant.MainState.ClearLuckRecruit, new ClearLuckRecruitState(), this);
        this._mainFsm.addState(Constant.MainState.LuckRoleEnter, new LuckRoleEnterState(), this);

        this._mainFsm.addState(Constant.MainState.LastRecruitPrepare, new LastRecruitPrepareState(), this);
        this._mainFsm.addState(Constant.MainState.LastRecruitStart, new LastRecruitStartState(), this);




        this._mainFsm.goToState(Constant.MainState.Prepare, this);
    }
    private onRecruitClick() {
        AudioMgr.inst.playOneShot(this.buttonClick);
        this._mainFsm.goToState(Constant.MainState.FirstRecruitStart, this);
    }
    private onRecruit1Click() {
        AudioMgr.inst.playOneShot(this.buttonClick);
        this._mainFsm.goToState(Constant.MainState.SecondRecruitStart, this);
    }
    private onRecruit2Click() {
        AudioMgr.inst.playOneShot(this.buttonClick);
        this._mainFsm.goToState(Constant.MainState.SecondRecruit1Start, this);
    }

    private onRecruit3Click() {
        AudioMgr.inst.playOneShot(this.buttonClick);
        this._mainFsm.goToState(Constant.MainState.LuckRecruitStart, this);
    }

    private onRecruit4Click() {
        AudioMgr.inst.playOneShot(this.buttonClick);
        this._mainFsm.goToState(Constant.MainState.LastRecruitStart, this);
    }

    update(deltaTime: number) {
        this._mainFsm?.tick(deltaTime);
    }

    //进度条已满 跳转
    public gameOver() {
        console.log('进度已满,试玩结束----------->');
    }
}


